package grape.house.rest.building.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.house.rest.building.form.BuildingListForm;
import grape.house.service.building.query.BuildingListQuery;
import grape.house.service.estate.api.IEstateService;
import grape.house.service.estate.po.Estate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.house.rest.building.form.BuildingCreateForm;
import grape.house.rest.building.form.BuildingUpdateForm;
import grape.house.rest.building.form.BuildingListPageForm;
import grape.house.rest.building.vo.BuildingVo;
import grape.house.rest.building.mapper.BuildingWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.house.service.building.po.Building;
import grape.house.service.building.api.IBuildingService;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 楼栋 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/building")
@Api(tags = "楼栋相关接口")
public class BuildingController extends BaseController<BuildingVo, Building> {

    @Autowired
    private BuildingWebMapper currentWebMapper;
    @Autowired
    private IBuildingService currentService;
    @Autowired
    private IEstateService iEstateService;


     @ApiOperation("添加楼栋")
     @PreAuthorize("hasAuthority('building:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public BuildingVo create(@RequestBody @Valid BuildingCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         Building po = currentWebMapper.formToPo(cf);
         extendEstateCompany(po);
         return super.create(po);
     }

     @ApiOperation("根据ID查询楼栋")
     @PreAuthorize("hasAuthority('building:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public BuildingVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除楼栋")
     @PreAuthorize("hasAuthority('building:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新楼栋")
     @PreAuthorize("hasAuthority('building:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public BuildingVo update(@PathVariable String id,@RequestBody @Valid BuildingUpdateForm uf) {
         Building po = currentWebMapper.formToPo(uf);
         extendEstateCompany(po);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询楼栋")
    @PreAuthorize("hasAuthority('building:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<BuildingVo> list(BuildingListForm listForm) {
        Building po = currentWebMapper.formToPo(listForm);
        BuildingListQuery buildingListQuery = new BuildingListQuery();
        buildingListQuery.setPoQuery(po);
        return super.list(buildingListQuery);
    }

    @ApiOperation("分页查询楼栋")
    @PreAuthorize("hasAuthority('building:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<BuildingVo> listPage(BuildingListPageForm listPageForm) {
         Building po = currentWebMapper.formToPo(listPageForm);
        BuildingListQuery buildingListQuery = new BuildingListQuery();
        buildingListQuery.setPoQuery(po);
         return super.listPage(buildingListQuery,listPageForm);
     }

    /**
     * 如果开发商和物业公司为空，继承楼盘的开发商和物业公司
     * @param po
     */
    private void extendEstateCompany(Building po) {
        // 如果开发商为空，继承楼盘的开发商
        if (isStrEmpty(po.getDeveloperCompanyId())) {
            Estate estate = iEstateService.getById(po.getEstateId());
            po.setDeveloperCompanyId(Optional.ofNullable(estate).map(estate1 -> estate.getDeveloperCompanyId()).orElse(null));
        }
        // 如果物业公司为空，继承楼盘的物业公司
        if (isStrEmpty(po.getPropertyCompanyId())) {
            Estate estate = iEstateService.getById(po.getEstateId());
            po.setPropertyCompanyId(Optional.ofNullable(estate).map(estate1 -> estate.getPropertyCompanyId()).orElse(null));
        }
    }

}
